package com.azzcs.myrpc.server;

import com.azzcs.myrpc.register.ServerRegister;
import com.azzcs.myrpc.request.RpcDecoder;
import com.azzcs.myrpc.request.RpcEncoder;
import com.azzcs.myrpc.request.RpcRequest;
import com.azzcs.myrpc.serializer.Serializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * @Author: wzg
 * @Date: 2021/1/22 下午3:54
 */
public class Server implements InitializingBean, DisposableBean {

    @Value("${rpc.server.port}")
    private int port;

    @Autowired
    private Serializer serializer;

    @Autowired
    private ApplicationContext context;

    @Value("${spring.application.name}")
    private String serviceName;

    @Autowired
    private ServerRegister serverRegister;

    @Override
    public void afterPropertiesSet() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();
        ServerBootstrap server = new ServerBootstrap();
        server.group(bossGroup,workGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>(){

                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new StringEncoder());
                        pipeline.addLast(new RpcDecoder(serializer));
                        pipeline.addLast(new ServiceHandler(context));
                    }
                });
        ChannelFuture f = server.bind(port).sync();
        System.out.println("server start success ...,port is :"+port);
        String address = InetAddress.getLocalHost().getHostAddress()+":"+port;
        serverRegister.register(serviceName,address);

        f.channel().closeFuture().sync();
    }

    @Override
    public void destroy() throws Exception {
        String address = InetAddress.getLocalHost().getHostAddress()+":"+port;
        serverRegister.remove(serviceName,address);
    }
}
